<template>
  <div>
    <h1>情况三：watch监视【reactive】定义的【对象类型】数据</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <button @click="changeName">修改姓名</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changePerson">修改整个人</button>
  </div>
</template>

<script setup>
import { reactive, watch } from "vue";
let person = reactive({
  name: "小王",
  age: 18,
});
function changeName() {
  person.name += "~";
}
function changeAge() {
  person.age += 1;
}
function changePerson() {
  // person = { name: "小张", age: 20 }; //reactive定义的对象不可以整体修改，只能修改属性
  Object.assign(person, { name: "张三", age: 20 }); //只能用Object.assign 浅拷贝--重新分配的意思
}
//情况三：watch监视【ref】定义的【对象类型】数据;且默认是开启深度监视，该深度监视无法关闭
watch(person, (newValue, oldValue) => {
  //newValue, oldValue这时候两个值一样，[地址值]没变
  console.log("person数据发生改变了", newValue, oldValue);
});
</script>

<style scoped></style>
